#include<STC15F2K60S2.h>
#include<intrins.h>
#define Uint unsigned int//无符号短整数
#define Ulong unsigned long int//无符号长整数
#define Byte unsigned char//字节
typedef Byte bool;
#define true  1
#define false 0
#define True  1
#define False 0

void delay(Ulong Time);
void SendString(Byte *Data);
void SendByte(Byte Data);
void UartInit(void);
void UartInit2(void);
void LED_Show(Byte *datas);
void ser();

sbit ROW1=P2^3;
sbit ROW2=P3^3;
sbit ROW3=P2^5;
sbit ROW4=P3^2;
sbit ROW5=P4^2;
sbit ROW6=P2^6;
sbit ROW7=P3^7;
sbit ROW8=P2^1;

sbit COLUMN_1=P3^5;
sbit COLUMN_2=P3^6;
sbit COLUMN_3=P4^4;
sbit COLUMN_4=P2^4;
sbit COLUMN_5=P2^0;
sbit COLUMN_6=P2^2;
sbit COLUMN_7=P3^4;
sbit COLUMN_8=P4^1;

Byte DEBUG_DATA[8]={1,2,4,8,16,32,64,128};
//Love1
Byte Love1_DATA[8]={0,102,153,129,129,66,36,24};
//Love2
Byte Love2_DATA[8]={102,153,129,129,66,36,24,0};
//Love3
Byte Love3_DATA[8]={255,129,129,129,129,129,129,255};
//Love4
Byte Love4_DATA[8]={255,129,129,129,129,129,129,255};

//isconnecting
bool CONNECTING=false;

//00:Normal 
Byte STATUS=0x00;
Byte Buff1[8]={0,0,0,0,0,0,0,0};
Byte Buff2[8]={0,0,0,0,0,0,0,0};

const Byte CMD_CONNECT=0x11;
const Byte CMD_DISCONNECT=0x10;
const Byte CMD_WRITEDATA=0x20;
Byte PROGRESS=0;
void main()
{
	Uint i=0;
	P2M0=255;
	P2M1=0x00;
	P3M0=252;
	P3M1=0x00;
	P4M0=0xFF;
	P4M1=0x00;
	
	UartInit();
	EA=1;
	ES=1;
	
	delay(10000);
	//SendString("Start!");
	while(1)
	{
		for(i=0;i<1000;i++)LED_Show(Love1_DATA);
		for(i=0;i<1000;i++)LED_Show(Love2_DATA);
		while(CONNECTING)LED_Show(Buff1);
	};
}

void ser() interrupt 4
{
	Byte i=0;
	Byte dat=0;
	if(RI!=1)return;
	dat=SBUF;
	RI=0;
	SendByte(dat);
	if(STATUS==0x00)
	{
		if(dat==CMD_CONNECT)CONNECTING=true;
		if(dat==CMD_DISCONNECT)CONNECTING=false;
		if(dat==CMD_WRITEDATA)
		{
			PROGRESS=0;
			STATUS=CMD_WRITEDATA;
		}
		return;
	}
	if(STATUS==CMD_WRITEDATA)
	{
		Buff2[PROGRESS]=dat;
		PROGRESS++;
		if(PROGRESS==8)
		{
			for(i=0;i<8;i++)Buff1[i]=Buff2[i];
			STATUS=0x00;
		}
		return;
	}
}

void LED_ColumnClose()
{
	COLUMN_1=1;
	COLUMN_2=1;
	COLUMN_3=1;
	COLUMN_4=1;
	COLUMN_5=1;
	COLUMN_6=1;
	COLUMN_7=1;
	COLUMN_8=1;
}

void LED_ColumnShow(Byte dat)
{
	COLUMN_1=!((dat%2)>0);
	COLUMN_2=!(((dat>>1)%2)>0);
	COLUMN_3=!(((dat>>2)%2)>0);
	COLUMN_4=!(((dat>>3)%2)>0);
	COLUMN_5=!(((dat>>4)%2)>0);
	COLUMN_6=!(((dat>>5)%2)>0);
	COLUMN_7=!(((dat>>6)%2)>0);
	COLUMN_8=!(((dat>>7)%2)>0);
}

void LED_Show(Byte *datas)
{
	Ulong delaytime=1;
	ROW1=0;
	ROW2=0;
	ROW3=0;
	ROW4=0;
	ROW5=0;
	ROW6=0;
	ROW7=0;
	ROW7=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[0]);
	ROW1=1;
	delay(delaytime);
	ROW1=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[1]);
	ROW2=1;
	delay(delaytime);
	ROW2=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[2]);
	ROW3=1;
	delay(delaytime);
	ROW3=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[3]);
	ROW4=1;
	delay(delaytime);
	ROW4=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[4]);
	ROW5=1;
	delay(delaytime);
	ROW5=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[5]);
	ROW6=1;
	delay(delaytime);
	ROW6=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[6]);
	ROW7=1;
	delay(delaytime);
	ROW7=0;
	
	LED_ColumnClose();
	LED_ColumnShow(datas[7]);
	ROW8=1;
	delay(delaytime);
	ROW8=0;
}

void delay(Ulong Time)//延时
{
	while(Time--){}
}

void SendString(Byte *Data)
{
	while(*Data){SendByte(*Data++);}
}

void SendByte(Byte Data)
{
	SBUF=Data;
	while(TI==0);//等待上一个字节发送完毕
	TI=0;
}

void UartInit(void)		//115200bps@11.0592MHz
{
	SCON = 0x50;		//8位数据,可变波特率
	AUXR |= 0x40;		//定时器1时钟为Fosc,即1T
	AUXR &= 0xFE;		//串口1选择定时器1为波特率发生器
	TMOD &= 0x0F;		//设定定时器1为16位自动重装方式
	TL1 = 0xE8;		//设定定时初值
	TH1 = 0xFF;		//设定定时初值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//启动定时器1
}

void UartInit2(void)		//115200bps@30.000MHz
{
	SCON = 0x50;		//8位数据,可变波特率
	AUXR |= 0x40;		//定时器1时钟为Fosc,即1T
	AUXR &= 0xFE;		//串口1选择定时器1为波特率发生器
	TMOD &= 0x0F;		//设定定时器1为16位自动重装方式
	TL1 = 0xBF;		//设定定时初值
	TH1 = 0xFF;		//设定定时初值
	ET1 = 0;		//禁止定时器1中断
	TR1 = 1;		//启动定时器1
}
